package com.ruoyi.web.controller.system;

import java.text.ParseException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DetectionCycle;
import com.ruoyi.system.domain.vo.PersonVo;
import com.ruoyi.system.domain.vo.checkExcelVo;
import com.ruoyi.system.mapper.PersonMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Person;
import com.ruoyi.system.service.IPersonService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * personController
 *
 * @author ruoyi
 * @date 2022-04-16
 */
@RestController
@RequestMapping("/system/person")
public class PersonController extends BaseController {
    @Autowired
    private IPersonService personService;
    @Autowired
    private PersonMapper personMapper;


    /**
     * 查询person列表
     */
    @PreAuthorize("@ss.hasPermi('system:person:list')")
    @GetMapping("/list")
    public TableDataInfo list(Person person) {
        List<Person> list1 = personMapper.selectPersonList(person);
        startPage();
        List<Person> list = personService.selectPersonList(person);
        int a = 0;
        int c = 0;
        for (Person p : list1) {
            if (p.getFalg() == 0) {  //已完成
                a++;
            }
            if (p.getFalg() == 1) {  //未完成
                c++;
            }
        }
        return getDataTablePerson(list, c + "", a + "");
    }


    /**
     * 导出
     */
    @PreAuthorize("@ss.hasPermi('system:person:export')")
    @Log(title = "导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Person person) {
        List<Person> list = personService.selectPersonList(person);
        for (Person person1 : list) {
            List<DetectionCycle> detectionCycleList = person1.getDetectionCycleList();
            if (detectionCycleList.size() > 0) {
                person1.setDetectionCycleName(detectionCycleList.get(0).getCategoryName());
                person1.setFrequencyYj(detectionCycleList.get(0).getFrequency() + "");
            }
            if (StringUtils.isNotEmpty(person1.getSource())) {
                if (person1.getSource().equals("0")) {
                    person1.setSource("pc新增");
                }
                if (person1.getSource().equals("1")) {
                    person1.setSource("pc导入");
                }
                if (person1.getSource().equals("2")) {
                    person1.setSource("小程序录入");
                }
                if (person1.getSource().equals("3")) {
                    person1.setSource("小程序经营者录入");
                }
            }
        }
        ExcelUtil<Person> util = new ExcelUtil<Person>(Person.class);
        return util.exportExcel(list, "防控信息数据");
    }

    /**
     * 获取person详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:person:query')")
    @GetMapping(value = "/{personId}")
    public AjaxResult getInfo(@PathVariable("personId") Long personId) throws ParseException {
        return AjaxResult.success(personService.selectPersonByPersonId(personId));
    }

    /**
     * 新增person
     */
    @PreAuthorize("@ss.hasPermi('system:person:add')")
    @Log(title = "person", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Person person) {
        return toAjax(personService.insertPerson(person));
    }

    /**
     * 修改person
     */
    @PreAuthorize("@ss.hasPermi('system:person:edit')")
    @Log(title = "person", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Person person) {
        return toAjax(personService.updatePerson(person));
    }

    /**
     * 删除person
     */
    @PreAuthorize("@ss.hasPermi('system:person:remove')")
    @Log(title = "person", businessType = BusinessType.DELETE)
    @PostMapping("/removes")
    public AjaxResult remove(@RequestBody Person person) {
        return toAjax(personService.deletePersonByPersonIds(person));
    }

    /**
     * 疫情防控信息导入模板下载
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:person:import')")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<PersonVo> util = new ExcelUtil<PersonVo>(PersonVo.class);
        return util.importTemplateExcel("防控信息");
    }

    /**
     * 导入
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "疫情管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<PersonVo> util = new ExcelUtil<PersonVo>(PersonVo.class);
        List<PersonVo> personList = util.importExcel(file.getInputStream());
        String message = personService.imporPerson(personList, updateSupport);
        return AjaxResult.success(message);
    }


    /**
     * 疫情防控信息批量删除重点人群
     *
     * @return
     */
    @GetMapping("/truncateTable")
    public AjaxResult truncateTable() {
        int i = personService.truncateTable();
        if (i == 0) {
            return toAjax(1);
        }
        return toAjax(i);
    }

    /**
     * 导出校验数据
     */
    @PreAuthorize("@ss.hasPermi('system:person:checkexcel')")
    @Log(title = "导出", businessType = BusinessType.EXPORT)
    @PostMapping("/checkExcel")
    public AjaxResult checkExcel(MultipartFile file, boolean updateSupport) throws Exception {
        //导入模板
        ExcelUtil<PersonVo> util = new ExcelUtil<PersonVo>(PersonVo.class);
        List<PersonVo> personList = util.importExcel(file.getInputStream());
        //导出报错Excel数据信息
        List<checkExcelVo> checkExcelVos = personService.checkExcel(personList, updateSupport);
        ExcelUtil<checkExcelVo> utils = new ExcelUtil<checkExcelVo>(checkExcelVo.class);
        return utils.exportExcel(checkExcelVos, "错误数据");
    }
}
